<?php namespace App\Core\Repository\Criterias\Filters;

use App\Core\Repository\Criteria;
use App\Core\Repository\Repository;
use Input;

class InputCriteria extends Criteria{
    
    protected $field;
    protected $input;
    protected $operator;
    protected $rules;
    
    public function __construct($field, $input = '', $default = '', $operator = '=', $rules = '') {
        $this->input = Input::get(($input ? $input : $field), $default);
        $this->field = $field ? $field : $input;
        $this->operator = $operator;
        $this->rules = $rules;
        $this->setName('filter_input_'.$this->field);
    }

    public function apply(Repository $repository){
        parent::apply($repository);
        if($this->input != ''){
            $this->query->where($this->formatField($this->model, $this->field), $this->operator, $this->input);
        }
    }
}